package server.net.packets;

import java.nio.ByteBuffer;

import server.Main;

public class Packet {

	private ByteBuffer payload;
	
	public Packet(ByteBuffer payload) {
		this.payload = payload;
	}
	
	public void decode() {
		byte packetId = readByte();
		Main.getPacketHandlers().getPacketHandler(packetId).readPayloadData(this);
	}
	
	public void handle() {
		
	}
	
	public void putString(String s) {
		payload.put(s.getBytes()) ;
		payload.put((byte)0);
	}
	
	public String readString() {
		StringBuilder sb = new StringBuilder();
		byte b;
		while((b = payload.get()) != 0) {
			sb.append(b);
		}
		return sb.toString();
	}
	
	public void putInt(int i) {
		payload.putInt(i);
	}
	
	public int readInt() {
		return payload.getInt();
	}
	
	public void putByte(byte b) {
		payload.put(b);
	}
	
	public byte readByte() {
		return payload.get();
	}
	
}
